#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SkeletonReader.h"
#include "SkeletonAnimationReader.h"
#include "FileReader.h"
#include "BoneList.h"

Bone * SkeletonReader::LoadFromFile(char * fileName, SkeletonAnimationManager * skeletonAnimationManager, TextureManager * textureManager, Vector2D * size)
{
	FILE * fptr = NULL;
	if((fptr = fopen(fileName, "r")) == NULL)
	{
		printf("Error opening skeleton file -%s-\n", fileName);
		return NULL;
	}
	
	char * tag;
	Bone * skeleton = NULL;
	while((tag = FileReader::GetNextTag(fptr)) != NULL)
	{
		printf("TAG: %s\n", tag);
		//read through the tag, one element at a time
		int pos = 0;		
		char * element = FileReader::GetNextElement(tag, &pos);
		
		printf("ELEMENT: %s\n", element);
		
		switch(GetElementType(element))
		{
			case ANIMATION_FILE_TYPE: 
				HandleAnimationFileTag(skeletonAnimationManager,tag,pos);
				break;
			case POSE_TYPE:
				printf("Reading POSE tag\n");
				if(!skeleton)
					skeleton = HandleSkeletonTag(tag,pos, textureManager);
				printf("Done reading pose tag\n");
				break;
			case SIZE_TYPE:
				*size = FileReader::ReadNextCoordinate(tag, &pos);
				break;
		}
		if(element != NULL)
			free(element);
		element = NULL;
		if(tag != NULL)
			free(tag);
		tag = NULL;
	}
	fclose(fptr);	
	return skeleton;
}
int SkeletonReader::GetElementType(char * element)
{
	if(strcmp(element, "animation") == 0)
		return ANIMATION_FILE_TYPE;
	if(strcmp(element,"pose") == 0)
		return POSE_TYPE;
	if(strcmp(element, "size") == 0)
		return SIZE_TYPE;
	return UNKNOWN_TYPE;
}
void SkeletonReader::HandleAnimationFileTag(SkeletonAnimationManager * skeletonAnimationManager, char * tag, int pos)
{
	char * fileName = FileReader::ReadNextString(tag, &pos);
	if(fileName)
	{
		char * name = NULL;
		SkeletonAnimation * animation = SkeletonAnimationReader::LoadFromFile(fileName, &name);
		if(animation)
		{
			skeletonAnimationManager->Add(name, animation);
			printf("animation added\n");
		}
	}
}
Bone * SkeletonReader::HandleSkeletonTag(char * tag, int pos, TextureManager * textureManager)
{
	printf("Handling pose tag\n");
	return ReadNextPose(tag, &pos, textureManager);	
}
Bone * SkeletonReader::ReadNextPose(char * tag, int * pos, TextureManager * textureManager)
{
	printf("Reading Bone\n");
	
	char * textureName = FileReader::ReadNextString(tag, pos);
	Vector2D * textureVertices = FileReader::ReadNextVectorArray(4, tag, pos);
	Vector2D size = FileReader::ReadNextCoordinate(tag, pos);
	float skinLocation = FileReader::ReadNextFloat(tag,pos);
	BoneOrientation * boneOrientation = SkeletonAnimationReader::ReadNextBoneOrientation(tag, pos);
	int numOutgoingBones = FileReader::ReadNextInt(tag, pos);
	
	Bone * bone = new Bone(*boneOrientation, textureManager, textureName,textureVertices, size, skinLocation);
	
	printf("Orientation = (%f, %f)\n", boneOrientation->GetLength(), boneOrientation->GetAngle());
	printf("Try to read %d more bones\n", numOutgoingBones);
	
	BoneList * list = new BoneList();
	for(int i = 0; i < numOutgoingBones; i++)
	{
		/*bone->AddBone*/list->Add(ReadNextPose(tag, pos, textureManager));
		printf("Bone added\n");
	}
	List<Bone> * search = list->head;
	while(search)
	{
		printf("searching\n");
		bone->AddBone(search->payload);
		search=search->next;	
	}
	return bone;
}
